package com.lc.hongtai.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lc.hongtai.MyApplication;
import com.lc.hongtai.R;
import com.lc.hongtai.activity.BankcardManagementActivity;
import com.lc.hongtai.activity.BorrowManageDetailActivity;
import com.lc.hongtai.activity.ExperienceActivity;
import com.lc.hongtai.activity.InvestManageActivity;
import com.lc.hongtai.activity.InviteFriendsActivity;
import com.lc.hongtai.activity.LoginActivity;
import com.lc.hongtai.activity.MyAssetsActivity;
import com.lc.hongtai.activity.NewWithDrawActivity;
import com.lc.hongtai.activity.PasswordManageActivity;
import com.lc.hongtai.activity.PersonalSettingsActivity;
import com.lc.hongtai.activity.RegistWebActivity_return;
import com.lc.hongtai.activity.RegisterActivity;
import com.lc.hongtai.activity.SettingsActivity;
import com.lc.hongtai.activity.WithPayActivity;
import com.lc.hongtai.adapter.MyGridAdapter;
import com.lc.hongtai.beans.MEventBeans;
import com.lc.hongtai.conn.JsonAccessToken;
import com.lc.hongtai.conn.JsonUserinfo;
import com.lc.hongtai.toast.LovelyToast;
import com.zcx.helper.activity.AppV4Fragment;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;

import de.greenrobot.event.EventBus;


/**
 * Created by Administrator on 2017/3/16.
 */
public class MineFragment extends AppV4Fragment implements View.OnClickListener {

    @BoundView(R.id.mine_setting_iv)
    private ImageView mine_setting_iv;//设置
    /*    @BoundView(R.id.mine_login_show_ll)
        private LinearLayout mine_login_show_ll;//登录显示布局*/
    @BoundView(R.id.mine_head_iv)
    private SimpleDraweeView mine_head_iv;//头像
    @BoundView(R.id.mine_money_tv)
    private TextView mine_money_tv;//总资产

    //登陆里面的布局

    @BoundView(R.id.mine_login_not_show_rl)
    private LinearLayout mine_login_not_show_rl;//未登录显示布局
    @BoundView(R.id.main_login_tv)
    private TextView main_login_tv;//登录按钮
    @BoundView(R.id.mine_recharge_tv)
    private TextView mine_recharge_tv;//充值
    @BoundView(R.id.mine_withdraw_tv)
    private TextView mine_withdraw_tv;//提现


    @BoundView(R.id.mine_invest_ll)
    private LinearLayout mine_invest_ll;   //投资管理
    /* @BoundView(R.id.mine_borrow_ll)
     private LinearLayout mine_borrow_ll;*///借款管理
    @BoundView(R.id.mine_assets_ll)
    private LinearLayout mine_assets_ll; //我的资产

    private int myType; //当主页 eventbus发送过来消息时候才可以跳转

    public static String userId;

    @BoundView(R.id.text_username)
    private TextView text_username;
    @BoundView(R.id.gridview)
    private GridView gridview;

    @BoundView(R.id.layout_zhaiquan)
    private LinearLayout layout_zhaiquan;

    @BoundView(R.id.text_code)
    private TextView text_code;

    //收益布局

    @BoundView(R.id.layout_shouye)
    private LinearLayout layout_shouye;
    //总资产
    @BoundView(R.id.layout_allmoney)
    private LinearLayout layout_allmoney;

    @BoundView(R.id.layout_invest_friend)
    private LinearLayout layout_invest_friend;
//银行卡管理

    @BoundView(R.id.layout_bankcard)
    private LinearLayout layout_bankcard;

    //密码设置

    @BoundView(R.id.layout_pwd_settings)
    private LinearLayout layout_pwd_settings;

    @BoundView(R.id.layout_tiyanjin)
    private LinearLayout layout_tiyanjin;

    //注册控件

    @BoundView(R.id.main_register_tv)
    private TextView main_register_tv;

    //昨日收益
    private TextView mine_yesterday_money;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_mine, null)));

        EventBus.getDefault().register(this);

        setListener();

        return view;
    }

    // 111 是mainActivity发送过来的  112是LoginActivity发送过来的, 0 setttingActivity退出时候发送的
    public void onEventMainThread(MEventBeans mEventBeans) {

        Log.e("---Mine", "EventBus 接收：" + mEventBeans.type);
        if (mEventBeans.type == 111) {

            //    startActivity(new Intent(getActivity(), RegistWebActivity.class).putExtra("url", MyApplication.myPreferences.readRegistUrl()));

        }

        myType = mEventBeans.type;

    }

    private void setListener() {
        mine_setting_iv.setOnClickListener(this);
        mine_head_iv.setOnClickListener(this);
        main_login_tv.setOnClickListener(this);
        mine_withdraw_tv.setOnClickListener(this);
        mine_recharge_tv.setOnClickListener(this);
        gridview.setAdapter(new MyGridAdapter(getContext()));
        //投资管理
        mine_invest_ll.setOnClickListener(this);

        //我的资产
        mine_assets_ll.setOnClickListener(this);

        layout_zhaiquan.setOnClickListener(this);
        layout_invest_friend.setOnClickListener(this);
        layout_bankcard.setOnClickListener(this);
        layout_pwd_settings.setOnClickListener(this);

        //体验金
        layout_tiyanjin.setOnClickListener(this);
        //注册
        main_register_tv.setOnClickListener(this);
    }

    @Override
    public void onResume() {

        if (!TextUtils.isEmpty(MyApplication.myPreferences.readUid())) {

            mine_login_not_show_rl.setVisibility(View.GONE);
            //   mine_login_show_ll.setVisibility(View.VISIBLE);
            mine_login_not_show_rl.setVisibility(View.GONE);
            text_username.setVisibility(View.VISIBLE);
            text_code.setVisibility(View.VISIBLE);
            layout_shouye.setVisibility(View.VISIBLE);
            layout_allmoney.setVisibility(View.VISIBLE);
            getAccessToken();

        } else {

            mine_login_not_show_rl.setVisibility(View.VISIBLE);
            // mine_login_show_ll.setVisibility(View.GONE);
            text_username.setVisibility(View.GONE);
            text_code.setVisibility(View.GONE);
            layout_shouye.setVisibility(View.INVISIBLE);
            layout_allmoney.setVisibility(View.INVISIBLE);
            mine_head_iv.setImageResource(R.mipmap.defaulticon);
        }

        super.onResume();
    }


    @Override
    public void onDetach() {
        super.onDetach();
        if (!TextUtils.isEmpty(MyApplication.myPreferences.readUid())) {

            mine_login_not_show_rl.setVisibility(View.GONE);
            //   mine_login_show_ll.setVisibility(View.VISIBLE);
            mine_login_not_show_rl.setVisibility(View.GONE);
            text_username.setVisibility(View.VISIBLE);
            text_code.setVisibility(View.VISIBLE);
            layout_shouye.setVisibility(View.VISIBLE);
            layout_allmoney.setVisibility(View.VISIBLE);
            getAccessToken();

        } else {

            mine_login_not_show_rl.setVisibility(View.VISIBLE);
            // mine_login_show_ll.setVisibility(View.GONE);
            text_username.setVisibility(View.GONE);
            text_code.setVisibility(View.GONE);
            layout_shouye.setVisibility(View.INVISIBLE);
            layout_allmoney.setVisibility(View.INVISIBLE);
        }

        super.onResume();
    }

    @Override
    public void onClick(View v) {

        if (v.getId() != R.id.main_login_tv && v.getId() != R.id.main_register_tv && TextUtils.isEmpty(MyApplication.myPreferences.readUid())) {
            //  ToastUtils.show(getActivity(), "请登录后再操作！");
            LovelyToast.makeText(getContext(), "请登录后在操作！", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
            return;
        }
        switch (v.getId()) {

            case R.id.mine_setting_iv://设置

                startActivity(new Intent(getActivity(), SettingsActivity.class));

                break;

            case R.id.mine_head_iv://头像

//                startActivity(new Intent(getActivity(), AccountManagementActivity.class));
                startActivity(new Intent(getActivity(), PersonalSettingsActivity.class));

                break;

            case R.id.main_login_tv://登录按钮

                startActivity(new Intent(getActivity(), LoginActivity.class));

                break;

            //注册按钮
            case R.id.main_register_tv:

                startActivity(new Intent(getActivity(), RegisterActivity.class));
                break;


            case R.id.mine_recharge_tv://充值按钮

                //false为充值
                //    startActivity(new Intent(getActivity(), WithdrawOrRechargeActivity.class).putExtra("mode", false));

                startActivity(new Intent(getActivity(), WithPayActivity.class));

                break;

            case R.id.mine_withdraw_tv://提现

                //true为提现
                //  startActivity(new Intent(getActivity(), WithdrawOrRechargeActivity.class).putExtra("mode", true));

                startActivity(new Intent(getActivity(), NewWithDrawActivity.class));

                break;
            case R.id.mine_invest_ll://投资管理

                startActivity(new Intent(getActivity(), InvestManageActivity.class));

                break;

            case R.id.mine_assets_ll://我的资产

                startActivity(new Intent(getActivity(), MyAssetsActivity.class));

                break;
//债权管理
            case R.id.layout_zhaiquan:

                startActivity(new Intent(getActivity(), BorrowManageDetailActivity.class));
                break;
//邀请好友

            case R.id.layout_invest_friend:
                startActivity(new Intent(getActivity(), InviteFriendsActivity.class));
                break;
//银行卡管理
            case R.id.layout_bankcard:
                startActivity(new Intent(getActivity(), BankcardManagementActivity.class));

                break;

            //密码设置
            case R.id.layout_pwd_settings:
                startActivity(new Intent(getActivity(), PasswordManageActivity.class));
                break;


            //体验金

            case R.id.layout_tiyanjin:

                startActivity(new Intent(getActivity(), ExperienceActivity.class));
                break;

        }
    }

    private void getAccessToken() {
        new JsonAccessToken("user/userinfo", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        onGetUserInfo(info.access_token);
                    } else {
                        //UtilToast.show(getActivity(), "安全验证失败！");
                        LovelyToast.makeText(getContext(), "安全验证失败！", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }

                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                // UtilToast.show(getActivity(), "安全验证失败！");
                LovelyToast.makeText(getContext(), "安全验证失败！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }).execute(getActivity(), false);
    }

    private void onGetUserInfo(String access_token) {
        new JsonUserinfo(access_token, MyApplication.myPreferences.readUid(), new AsyCallBack<JsonUserinfo.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonUserinfo.Info info) throws Exception {
                if (TextUtils.isEmpty(info.url)) {
                  //http:\/\/vhost906.zihaistar.com\/api\/common\/url?uid=3010
                    //  startActivity(new Intent(getActivity(), RegistWebActivity_return.class).putExtra("url", info.url));

                    startActivity(new Intent(getActivity(), RegistWebActivity_return.class).putExtra("url", "http://vhost906.zihaistar.com/api/common/url?uid="+info.id));

                } else {
                    MyApplication.myPreferences.saveUid(info.id);

                    MyApplication.myPreferences.saveUserName(info.user_name);

                    MyApplication.myPreferences.saveUser_email(info.user_email);

                    MyApplication.myPreferences.saveUser_phone(info.user_phone);

                    MyApplication.myPreferences.saveSex(info.sex);

                    MyApplication.myPreferences.saveReal_name(info.real_name);

                    MyApplication.myPreferences.saveIdcard(info.idcard);

                    MyApplication.myPreferences.saveMoney_freeze(info.money_freeze);

                    MyApplication.myPreferences.saveMoney_collect(info.money_collect);

                    MyApplication.myPreferences.saveAccount_money(info.account_money);

                    MyApplication.myPreferences.saveMoney_experience(info.money_experience);

                    MyApplication.myPreferences.saveAll_money(info.all_money);

                    MyApplication.myPreferences.saveUserpic(info.userpic);

                    MyApplication.myPreferences.saveId_status(info.id_status);

                    MyApplication.myPreferences.saveVip_status(info.vip_status);

                    MyApplication.myPreferences.savePhone_status(info.phone_status);

                    MyApplication.myPreferences.saveEmail_status(info.email_status);

                    MyApplication.myPreferences.savePin_pass(info.pin_pass);
                    MyApplication.myPreferences.saveNickName(info.nick_name);

                    mine_money_tv.setText(MyApplication.myPreferences.readAll_money());

                    mine_head_iv.setImageURI(Uri.parse(MyApplication.myPreferences.readUserpic()));
                    text_username.setText(MyApplication.myPreferences.readUserName());


//                        MyApplication.myPreferences.saveRegistUrl(info.url);
                }


                /*   if (info != null) {
                    if (TextUtils.isEmpty(info.url)) {
                        MyApplication.myPreferences.saveUid(info.id);

                        MyApplication.myPreferences.saveUserName(info.user_name);

                        MyApplication.myPreferences.saveUser_email(info.user_email);

                        MyApplication.myPreferences.saveUser_phone(info.user_phone);

                        MyApplication.myPreferences.saveSex(info.sex);

                        MyApplication.myPreferences.saveReal_name(info.real_name);

                        MyApplication.myPreferences.saveIdcard(info.idcard);

                        MyApplication.myPreferences.saveMoney_freeze(info.money_freeze);

                        MyApplication.myPreferences.saveMoney_collect(info.money_collect);

                        MyApplication.myPreferences.saveAccount_money(info.account_money);

                        MyApplication.myPreferences.saveMoney_experience(info.money_experience);

                        MyApplication.myPreferences.saveAll_money(info.all_money);

                        MyApplication.myPreferences.saveUserpic(info.userpic);

                        MyApplication.myPreferences.saveId_status(info.id_status);

                        MyApplication.myPreferences.saveVip_status(info.vip_status);

                        MyApplication.myPreferences.savePhone_status(info.phone_status);

                        MyApplication.myPreferences.saveEmail_status(info.email_status);

                        MyApplication.myPreferences.savePin_pass(info.pin_pass);
                        MyApplication.myPreferences.saveNickName(info.nick_name);

                        mine_money_tv.setText(MyApplication.myPreferences.readAll_money());

                        mine_head_iv.setImageURI(Uri.parse(MyApplication.myPreferences.readUserpic()));
                        text_username.setText(MyApplication.myPreferences.readUserName());


//                        MyApplication.myPreferences.saveRegistUrl(info.url);
                    } else {

                        startActivity(new Intent(getActivity(), RegistWebActivity_return.class).putExtra("url", info.url));
                        //   EventBus.getDefault().post(9);

                        return;

                    }


                }
*/

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                // UtilToast.show(getActivity(), JsonUserinfo.TOAST);
                LovelyToast.makeText(getContext(), JsonUserinfo.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

        }

        ).

                execute(getActivity(),

                        false);

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
